/**
 * 
 */
package com.effectivsocial.core.jsf.extension;

import java.util.Locale;
import java.util.Map;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

/**
 * Use Faces class of Omnifaces instead
 * @author Dhrubo
 * 
 */
@Deprecated
public abstract class FacesUtils {

	public static Locale getLocale(FacesContext context) {
		if (context == null) {
			context = FacesContext.getCurrentInstance();
		}
		Locale locale = null;
		UIViewRoot viewRoot = context.getViewRoot();
		if (viewRoot != null)
			locale = viewRoot.getLocale();
		if (locale == null)
			locale = Locale.getDefault();
		return locale;
	}

	public static String getRequestParameter(String name) {
		return (String) getRequestParameterMap().get(name);
	}

	public static Long getLongRequestParameter(String name) {
		String paramValue = getRequestParameter(name);

		Long retVal = null;
		if (StringUtils.isNotBlank(paramValue)) {
			retVal = Long.valueOf(getRequestParameter(name));
		}

		return retVal;
	}

	public static boolean getBooleanRequestParameter(String name) {
		String paramValue = getRequestParameter(name);

		Boolean retVal = false;
		if (StringUtils.isNotBlank(paramValue)) {
			retVal = Boolean.valueOf(getRequestParameter(name));
		}

		return retVal;
	}

	public static boolean isRequestParameterPresent(String name) {
		return getRequestParameterMap().containsKey(name);
	}

	public static String getViewRoot() {
		return FacesContext.getCurrentInstance().getViewRoot().getViewId();
	}

	public static Map<String, String> getRequestParameterMap() {
		return FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap();
	}

	public static String getBrowser() {
		return FacesContext.getCurrentInstance().getExternalContext()
				.getRequestHeaderMap().get("User-Agent");
	}

	public static Flash flashScope() {
		return (FacesContext.getCurrentInstance().getExternalContext()
				.getFlash());
	}

	public static String getRequestedUrl() {
		FacesContext ctx = FacesContext.getCurrentInstance();
		HttpServletRequest servletRequest = (HttpServletRequest) ctx
				.getExternalContext().getRequest();

		// returns something like "/myapplication/home.faces"
		String fullURI = servletRequest.getRequestURI();
		return fullURI;
	}

	public static void setSessionAttribute(String key, Object value) {
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) context.getExternalContext()
				.getSession(false);
		session.setAttribute(key, key);
	}

	public static Object getSessionAttribute(String key) {
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) context.getExternalContext()
				.getSession(false);
		return session.getAttribute(key);
	}

}